<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateFreightAreaRequest;
use App\Http\Requests\UpdateFreightAreaRequest;
use App\Repositories\FreightAreaRepository;
use App\Http\Controllers\AppBaseController;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class FreightAreaController extends AppBaseController
{
    /** @var  FreightAreaRepository */
    private $freightAreaRepository;

    public function __construct(FreightAreaRepository $freightAreaRepo)
    {
        $this->freightAreaRepository = $freightAreaRepo;
    }

    /**
     * Display a listing of the FreightArea.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->freightAreaRepository->pushCriteria(new RequestCriteria($request));
        $freightAreas = $this->freightAreaRepository->all();

        return view('freight_areas.index')
            ->with('freightAreas', $freightAreas);
    }

    /**
     * Show the form for creating a new FreightArea.
     *
     * @return Response
     */
    public function create()
    {
        return view('freight_areas.create');
    }

    /**
     * Store a newly created FreightArea in storage.
     *
     * @param CreateFreightAreaRequest $request
     *
     * @return Response
     */
    public function store(CreateFreightAreaRequest $request)
    {
        $input = $request->all();

        $freightArea = $this->freightAreaRepository->create($input);

        Flash::success('Freight Area saved successfully.');

        return redirect(route('freightAreas.index'));
    }

    /**
     * Display the specified FreightArea.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $freightArea = $this->freightAreaRepository->findWithoutFail($id);

        if (empty($freightArea)) {
            Flash::error('Freight Area not found');

            return redirect(route('freightAreas.index'));
        }

        return view('freight_areas.show')->with('freightArea', $freightArea);
    }

    /**
     * Show the form for editing the specified FreightArea.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $freightArea = $this->freightAreaRepository->findWithoutFail($id);

        if (empty($freightArea)) {
            Flash::error('Freight Area not found');

            return redirect(route('freightAreas.index'));
        }

        return view('freight_areas.edit')->with('freightArea', $freightArea);
    }

    /**
     * Update the specified FreightArea in storage.
     *
     * @param  int              $id
     * @param UpdateFreightAreaRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateFreightAreaRequest $request)
    {
        $freightArea = $this->freightAreaRepository->findWithoutFail($id);

        if (empty($freightArea)) {
            Flash::error('Freight Area not found');

            return redirect(route('freightAreas.index'));
        }

        $freightArea = $this->freightAreaRepository->update($request->all(), $id);

        Flash::success('Freight Area updated successfully.');

        return redirect(route('freightAreas.index'));
    }

    /**
     * Remove the specified FreightArea from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $freightArea = $this->freightAreaRepository->findWithoutFail($id);

        if (empty($freightArea)) {
            Flash::error('Freight Area not found');

            return redirect(route('freightAreas.index'));
        }

        $this->freightAreaRepository->delete($id);

        Flash::success('Freight Area deleted successfully.');

        return redirect(route('freightAreas.index'));
    }
}
